package com.wntms.wy.web;

import com.github.pagehelper.PageInfo;
import com.wntms.wy.pojo.Result;
import com.wntms.wy.pojo.CustomerAccept;
import com.wntms.wy.pojo.sjhz.DriverBill_s;
import com.wntms.wy.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
@CrossOrigin
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    //应该是前端填入货运单号，有个离焦事件，先去数据库查一下看看有没有这个货运单，有无货物代收，代收是否结算
    //判断是否能填写客户验收单
    @GetMapping("/panduanIsOK/{fid}")
    private Result panduanIsOK(@PathVariable String fid){
        Boolean hasFreightContract = customerService.isHasFreightContract(fid);
        Result result = new Result();
        if (hasFreightContract){
            //判断有了货运单后再判断有没有货款代收
            Boolean hasPayment = customerService.isHasPayment(fid);
            if (hasPayment){
                //有代收货款，再判断一下是否结算
                Boolean payment = customerService.isPayment(fid);
                if (payment){
                    //说明货款已付清,返货true
                    result.setCode(20001);
                    result.setMsg("货运单正确，货款已付清。可以继续填写。");
                }else {
                    //说明贷款还没有付清，返回false
                    result.setCode(50001);
                    result.setMsg("贷款还没有付清，请到结算管理结算！");
                }
            }else {
                //说明没有代收款
                result.setCode(20001);
                result.setMsg("货运单正确可以继续填写。");
            }
        }else {
            //没有该货运单
            result.setCode(50001);
            result.setMsg("没有该货运单，请重新录入单号！");
        }
        return result;
    }//写复杂了，应该写在业务逻辑层
    //有了上面的判断接下来可以增加客户验收单了
    @PostMapping
    public Result addCustomerAccept(@RequestBody CustomerAccept customerAccept){
        System.out.println();
        //System.out.println(customerAccept);

        System.out.println("我进来了吗");
        Boolean aBoolean = customerService.addCustomerAccept(customerAccept);
        Result result = new Result();
        if (aBoolean){
            //增加成功
            result.setCode(20001);
            result.setMsg("增加成功");
        }else {
            //增加失败
            result.setCode(50001);
            result.setMsg("增加失败");
        }
        return result;
    }
    @DeleteMapping("/{id}")
    public Result deleteCustomerAccept(@PathVariable Integer id){
        Boolean aBoolean = customerService.deleteCustomerAccept(id);
        Result result = new Result();
        if (aBoolean){
            //删除成功
            result.setCode(20001);
            result.setMsg("删除成功");
        }else {
            //删除失败
            result.setCode(50001);
            result.setMsg("删除失败");
        }
        return result;
    }
    @PutMapping
    public Result updateCustomerAccept(@RequestBody CustomerAccept customerAccept){
        Boolean aBoolean = customerService.updateCustomerAccept(customerAccept);
        System.out.println(customerAccept);
        Result result = new Result();
        if (aBoolean){
            //修改成功
            result.setCode(20001);
            result.setMsg("修改成功");
        }else {
            //修改失败
            result.setCode(50001);
            result.setMsg("修改失败");
        }
        return result;
    }
    @GetMapping("/{currentPage}/{pageSize}")
    public Result findAllCustomerAccept(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        List<CustomerAccept> list = customerService.findAllCustomerAccept(currentPage,pageSize);
        Result result = new Result();
        if (list != null){
            //说明还没有客户验收单
            //修改成功
            PageInfo<CustomerAccept> pageInfo = new PageInfo<>(list);
            result.setCode(20001);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else {
            //修改失败
            result.setCode(50001);
            result.setMsg("查询失败");
        }
        return result;
    }
}
